package com.sgb.kjwl.viewmodel.self.driver;

import android.app.Application;
import android.content.Context;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.kjwl.view.ui.activity.self.driver.NewProprietaryDriverActivity;
import com.sgb.kjwl.view.ui.activity.self.driver.UploadTransportQualificationCertificateActivity;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.common.base.BaseViewModel;
import com.swgk.core.dialog.DialogHelper;

import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * Describe:
 * Author:  yangLei
 * Date:    2021/10/15
 */
public class UploadTransportCertificateModel extends BaseViewModel {

    public MutableLiveData<Boolean> submitLD = new MutableLiveData<>();

    public UploadTransportCertificateModel(@NonNull @NotNull Application application) {
        super(application);
    }

    /**
     * 上传图片
     */
    public void uploadMutFileModel(UploadTransportQualificationCertificateActivity activity, File file, int type) {
        Map<String, RequestBody> photos = new HashMap<>();
        RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        photos.put("file" + "\"; filename=\"" + "image.jpg\"", requestBody);
        DialogHelper.showProgressDialog(activity, "文件上传中...").setCanceledOnTouchOutside(false);
        APIManager.getInstance().getUploadAPI().uploadFile(photos).enqueue(new ICallBack<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity data) {
                activity.uploadImgSuccess(data.getData().toString(), type);
            }
        });
    }

    /**
     * 上传道路运输从业资格证
     *
     * @param context
     * @param mainCertificate 主页
     * @param viceCertificate 副页
     * @param userNo          司机的userNo
     */
    public void uploadCertificate(Context context, String mainCertificate, String viceCertificate, String userNo) {
        Map<String, Object> map = new HashMap();
        map.put("mainCertificate", mainCertificate);
        map.put("viceCertificate", viceCertificate);
        map.put("userNo", userNo);
        NewAPIManager.getInstance().getLogisticsAPI().uploadTransportCertificate(map).enqueue(new ICallBack<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity data) {
                submitLD.postValue(true);
            }
        });
    }
}
